package com.link.play.fit.ai.ui.main.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.bean.InfoIngredientsBean
import com.link.play.fit.ai.bean.ServingTypeBean
import com.link.play.fit.ai.databinding.ItemFoodEditAcOtherNutritionBinding

// FoodEditAc 页面的adapter
class FoodEditAdapter : RecyclerView.Adapter<FoodEditAdapter.ViewHolder>() {


    var data = mutableListOf<Template>()
    var serving: Double = 1.0

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val binding = ItemFoodEditAcOtherNutritionBinding.inflate(
            LayoutInflater.from(parent.context),
            parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {
        holder.setData(data[position])
    }

    override fun getItemCount() = data.size

    fun setData(serving: Double, d: ServingTypeBean) {
        data = mutableListOf()
        data.add(
            Template(
                "Saturated Fat", d.saturatedFat, "g"
            )
        )
        data.add(
            Template(
                "PolyUnsaturated Fat", d.polyUnsaturatedFat, "g"
            )
        )
        data.add(
            Template(
                "MonoUnsaturated Fat", d.monoUnsaturatedFat, "g"
            )
        )
        data.add(
            Template(
                "Cholesterol", d.cholesterol, "mg"
            )
        )
        data.add(
            Template(
                "Sodium", d.sodium, "mg"
            )
        )
        data.add(
            Template(
                "Fiber", d.fiber, "g"
            )
        )
        data.add(
            Template(
                "Sugar", d.sugar, "g"
            )
        )
        data.add(
            Template(
                "Potassium", d.potassium, "mg"
            )
        )
        data.add(
            Template(
                "Vitamin A", d.vitaminA, "µg"
            )
        )
        data.add(
            Template(
                "Vitamin C", d.vitaminC, "mg"
            )
        )
        data.add(
            Template(
                "Calcium", d.calcium, "mg"
            )
        )
        data.add(
            Template(
                "Iron", d.calcium, "mg"
            )
        )
        this.serving = serving
        notifyDataSetChanged()
    }

    inner class ViewHolder(val item: ItemFoodEditAcOtherNutritionBinding) :
        RecyclerView.ViewHolder(item.root) {

        fun setData(t: Template) {
            item.tvLabel.text = t.name
            item.tvNum.text = "${(t.value * serving).toInt()}${t.unit}"
        }
    }

    data class Template(
        var name: String = "",
        var value: Double = 0.0,
        var unit: String = ""
    )
}